<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="must-revalidate">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store">
    <meta HTTP-EQUIV="Expires" CONTENT="0">
    <title>Goodwill - Поиск товаров клиентами</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <link rel="stylesheet" href="resources/css/clients.css"/>
    <style type="text/css">
        <%--Стили для колонок таблицы товаров--%>

        <%--Наименование--%>
        .c0 {
            font-weight: bold;
            background-color: whitesmoke;
            white-space: nowrap;
        }

        <%--Код--%>
        .c1 {
            font-weight: bold;
            background-color: whitesmoke;
            white-space: nowrap;
            width: 1%;
        }

        <%--о.е.--%>
        .c2 {
            white-space: nowrap;
            width: 1%;
        }

        <%--Применимость--%>
        .c3 {
            white-space: nowrap;

        }

        <%--В наличии--%>
        .c4 {
            background-color: whitesmoke;
            white-space: nowrap;
            width: 80px;
        }

        <%--Цена--%>
        .c5 {
            background-color: whitesmoke;
        }

        <%--Действия--%>
        .c6 {
        }

        .pr {
            width: 150px;
            padding-left: 5px;
            padding-right: 5px;
            text-align: center;
        }

        .star {
            font-weight: bold;
            font-size: 160%;
            vertical-align: top;
            color: red;
        }

        .hc1 {
            font-size: 12pt;
        }

        .rc1 {
            height: 25px;
        }

        .cc1, .cc2, .cc3, .cc4, .cc5, .cc6, .cc7 {
            padding-left: 5px;
            padding-right: 5px;
            padding-top: 3px;
            padding-bottom: 2px;
        }

        .cc1 {
        }

        .cc2 {
        }

        .cc3 {

        }

        .cc4 {

        }

        .cc5 {

        }

        .cc6 {

        }

        .cc7 {

        }

        .dr-sdt-row-active {
            color: red;
        }
    </style>
    <script type="text/javascript">
        function addGoodInOrder(goodId, goodCount, goodMaxCount) {
            try {
                if (isNaN(goodId) || isNaN(goodCount)) {
                    alert('Необходимо ввести число');
                    return;
                }

                _addGoodInOrder(goodId, goodCount, goodMaxCount);
            } catch (e) {
                alert(e);
            }
        }

        function onAddGoodInputKeyDown(event, goodId, goodCount, goodMaxCount, element) {
            try {
                if (event.keyCode == 13) {
                    addGoodInOrder(goodId, goodCount, goodMaxCount);
                    element.value = '0';
                }
            } catch (e) {
                alert(e);
            }
        }

        function msg_begin()
        {
            Richfaces.showModalPanel('modalMessages');
        }

        function msg_end()
        {
            Richfaces.hideModalPanel('modalMessages');
        }

        function hide_msgs()
        {
            setTimeout("msg_end()", 3000);
        }
    </script>
</head>
<body>
<f:view>
    <a4j:form id="jsForm">
        <a4j:jsFunction name="_addGoodInOrder" reRender="goodsInOrder,dvOrderItemsMap" action="#{ClientGoodsSearchBean.addGoodInOrder}" oncomplete="msg_begin();hide_msgs();">
            <a4j:actionparam name="goodId" assignTo="#{ClientGoodsSearchBean.goodId}"/>
            <a4j:actionparam name="goodCount" assignTo="#{ClientGoodsSearchBean.goodCount}"/>
            <a4j:actionparam name="goodMaxCount" assignTo="#{ClientGoodsSearchBean.goodMaxCount}"/>
        </a4j:jsFunction>
    </a4j:form>

    <jsp:include page="ClientHeaderWithoutSearch.jsp"/>

    <h:panelGrid columns="2">
        <h:panelGroup id="dvBack">
            <h:panelGroup rendered="#{ClientGoodsSearchBean.modeIsSearch}">
                <h:form>
                    <h:commandLink action="#{ClientGoodsSearchBean.searchByGoodType}" value="Вернуться к группе товаров #{ClientGoodsSearchBean.searchParamGoodTypeName}"/>
                </h:form>
            </h:panelGroup>
            <h:panelGroup rendered="#{ClientGoodsSearchBean.modeIsGoodType}">
                <h:form>
                    <h:selectOneMenu value="#{ClientGoodsSearchBean.searchParamGoodTypeId}" id="cbGoodTypes">
                        <f:selectItems value="#{ClientGoodsSearchBean.clientGroupsSelectItems}"/>
                        <a4j:support event="onchange"
                                     action="#{ClientGoodsSearchBean.searchByGoodType}"
                                     limitToList="true"
                                     ajaxSingle="true"
                                     process="cbGoodTypes"
                                     oncomplete="document.location='ClientGoodsSearchPage.jsf'"/>
                    </h:selectOneMenu>
                </h:form>
            </h:panelGroup>
        </h:panelGroup>
        <h:panelGroup layout="block" styleClass="search_pane">
            <h:form id="fClientSearch">
                <h:graphicImage value="/resources/img/search_16x16.png"/>
                <h:outputLabel value="Поиск по коду Goodwill"/>
                <h:inputText value="#{ClientGoodsSearchBean.searchParamOe}" style="margin-left:6px"/>
                <a4j:commandButton id="btnClientSearch" value="Поиск" action="#{ClientGoodsSearchBean.searchByOe}" oncomplete="document.location='ClientGoodsSearchPage.jsf'"/>
                <rich:hotKey key="return" handler="#{rich:element('btnClientSearch')}.click()" selector="#fClientSearch"/>
            </h:form>
        </h:panelGroup>
    </h:panelGrid>

    <jsp:include page="status.jsp"/>

    <%-- -------------- Сообщение об ошибки -------------- --%>
    <rich:modalPanel id="modalMessages" autosized="true" width="350">
        <h:panelGrid columns="1">
            <h:panelGroup>
                <jsp:include page="messages.jsp"/>
            </h:panelGroup>
            <h:panelGroup>
                <h:outputText value="Для просмотра заказа перейдите в "/>
                <h:outputLink value="#" onclick="document.location='ClientOrderItemPage.jsf'">
                    <h:outputText value="Корзину"/>
                </h:outputLink>
            </h:panelGroup>
            <h:outputLink value="#" onclick="msg_end()">
                <h:outputText value="Закрыть"/>
            </h:outputLink>
        </h:panelGrid>
    </rich:modalPanel>

    <%-- -------------- Список найденных товаров -------------- --%>
    <h:panelGroup id="tbClientsSearch">
        <%--<a4j:status id="s3"/>--%>
        <%--<a4j:form status="s3">--%>
            <rich:dataTable value="#{ClientGoodsSearchBean.goods}" var="good" styleClass="full"
                            width="100%"
                            headerClass="hc1"
                            rowClasses="rc1"
                            columnClasses="cc1,cc2,cc3,cc4,cc5,cc6,cc7"
                            rowKeyVar="row">
                <f:facet name="header">
                    <h:outputText value="#{ClientGoodsSearchBean.tableTitle}" style="font-size:14px;font-weight:bold;"/>
                </f:facet>
                <rich:column width="150">
                    <f:facet name="header">
                        <h:outputText value="Наименование" style="font-weight: bold;font-size: 10pt;"/>
                    </f:facet>
                    <h:outputText value="#{good.name}"/>
                </rich:column>
                <rich:column width="80">
                    <f:facet name="header">
                        <h:outputText value="Код" style="font-weight: bold;font-size: 10pt;"/>
                    </f:facet>
                    <h:outputText value="#{good.code}"/>
                </rich:column>
                <%--<rich:column width="300">--%>
                <%--<f:facet name="header">--%>
                <%--<h:outputText value="o.e." style="font-weight: bold;font-size: 10pt;"/>--%>
                <%--</f:facet>--%>
                <%--<h:outputText value="#{good.oeShort}"/>--%>
                <%--</rich:column>--%>
                <%--<rich:column width="300">--%>
                <%--<f:facet name="header">--%>
                <%--<h:outputText value="Применимость" style="font-weight: bold;font-size: 10pt;"/>--%>
                <%--</f:facet>--%>
                <%--<h:outputText value="#{good.prShort}" id="lPrShort"/>--%>
                <%--</rich:column>--%>
                <rich:column width="60px">
                    <f:facet name="header">
                        <h:outputText value="Корзина" style="font-weight: bold;font-size: 10pt;"/>
                    </f:facet>
                    <h:panelGroup id="dvOrderItemsMap">
                        <h:outputText value="#{SessionBean.orderItemsMap[good.id]}"/>
                    </h:panelGroup>
                </rich:column>
                <rich:column width="60px">
                    <f:facet name="header">
                        <h:outputText value="Кол-во" style="font-weight: bold;font-size: 10pt;"/>
                    </f:facet>
                    <h:panelGroup>
                        <h:outputText value="#{good.count}" id="good_count" style="display:none;"/>
                        <h:outputText value="#{good.reservedCount}" id="good_reservedCount" style="display:none;"/>
                        <h:outputText value="в наличии"
                                      rendered="#{(good.count - good.reservedCount) >= ClientGoodsSearchBean.viewGoodsCount}"
                                      style="color:darkgreen;"/>
                        <h:outputText value="#{good.count - good.reservedCount} шт."
                                      rendered="#{(good.count - good.reservedCount) > 0 && (good.count - good.reservedCount) < ClientGoodsSearchBean.viewGoodsCount}"
                                      style="color:darkgreen;"/>
                        <h:outputText value="#{good.count - good.reservedCount} шт."
                                      rendered="#{(good.count - good.reservedCount) == 0}" style="color:darkred;"/>
                    </h:panelGroup>
                </rich:column>
                <rich:column style="text-align: center;" width="60px">
                    <f:facet name="header">
                        <h:outputText value="Цена<span class=\"star\">*</span>" escape="false"
                                      style="font-weight: bold;font-size: 10pt;"/>
                    </f:facet>
                    <h:outputText value="#{good.pricesMap[ClientGoodsSearchBean.clientgroupsAssigment[good.brandId]]}"
                                  converter="MoneyConverter"/>
                </rich:column>
                <rich:column width="150px">

                    <h:panelGroup rendered="#{(good.count - good.reservedCount) > 0}">
                        <h:panelGroup id="add_good_count_pane">
                            <h:inputText id="add_good_count"
                                         value="#{0}"
                                         onclick="this.select();"
                                         onkeydown="onAddGoodInputKeyDown(event, #{good.id}, #{rich:element('add_good_count')}.value, #{good.count - good.reservedCount}, #{rich:element('add_good_count')})"
                                         style="width:35px"/>
                        </h:panelGroup>
                        <h:outputText value="шт." style="margin-left:2px;color:black"/>

                        <%--<a4j:commandLink--%>
                                <%--id="lAdd"--%>
                                <%--value="добавить"--%>
                                <%--title="Добавить указанное кол-во товара в корзину"--%>
                                <%--style="margin-left:2px"                                --%>
                                <%--onclick="addGoodInOrder(#{good.id}, #{rich:element('add_good_count')}.value, #{good.count - good.reservedCount}); #{rich:element('add_good_count')}.value = '0'; return false;"/>--%>
                        <h:outputLink
                                id="lAdd"
                                value="#"
                                title="Добавить указанное кол-во товара в корзину"
                                style="margin-left:2px"
                                onclick="addGoodInOrder(#{good.id}, #{rich:element('add_good_count')}.value, #{good.count - good.reservedCount}); #{rich:element('add_good_count')}.value = '0'; return false;">
                            <h:outputText value="добавить"/>
                        </h:outputLink>
                    </h:panelGroup>
                    <h:panelGroup rendered="#{(good.count - good.reservedCount) == 0}">
                        <h:outputText value="товар отсутствует на складе" style="color:dimgray;font-size:80%"/>
                    </h:panelGroup>

                </rich:column>
            </rich:dataTable>
        <%--</a4j:form>--%>
    </h:panelGroup>

    <jsp:include page="ClientFooter.jsp"/>

    <rich:modalPanel id="registrationInfoModalPanel" autosized="true" width="350">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Внимание!"/>
            </h:panelGroup>
        </f:facet>
        <f:facet name="controls">
            <a4j:commandLink onclick="#{rich:component('registrationInfoModalPanel')}.hide();return false;">
                <h:graphicImage value="/resources/img/close.png"/>
            </a4j:commandLink>
        </f:facet>
        <h:outputText value="Заказ возможен только после регистрации"/>
    </rich:modalPanel>

</f:view>
</body>
</html>